﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PlayerTest.Player.Event;

namespace PlayerTest.CustomControls
{
    /// <summary>
    /// ScriptItem.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class SectionItem : UserControl
    {
        private double start;
        private double end;
        private string thumb;

        #region Event

        public event SectionItemEventHandler eventHandler;
        /// <summary>
        /// Player에게 이벤트 전송
        /// </summary>
        /// <param name="arg"></param>
        private void SendEvent(SectionItemEventArgs arg)
        {
            // 연결된 모든 함수에 이벤트 전송
            SectionItemEventHandler handler = eventHandler;
            if (handler != null) handler(this, arg);
        }
        #endregion

        public string Thumb
        {
            get { return thumb; }
            set
            {
                thumb = value;

                Uri path = new Uri(string.Format("{0}\\thumbs\\{1}", Environment.CurrentDirectory, thumb),UriKind.RelativeOrAbsolute);
                /*
                BitmapImage img = new BitmapImage();

                img.BeginInit();
                img.CacheOption = BitmapCacheOption.OnDemand;
                img.CreateOptions = BitmapCreateOptions.DelayCreation;
                img.DecodePixelWidth = 300;
                img.UriSource = path;
                img.EndInit();
                */
                ThumbPicture.Source = new BitmapImage(path);
            }
        }
        public double Start
        {
            get { return start; }
            set
            {
                start = value;
                StartText.Content = TextTime(start);
            }
        }
        public double End
        {
            get { return end; }
            set
            {
                end = value;
                EndText.Content = TextTime(end);
            }
        }

        private static string TextTime(double time)
        {
            int pos = (int)time;
            int hour = pos / 3600;
            int minute = pos / 60;
            int sec = pos % 60;

            return string.Format("{0}:{1}:{2}", hour.ToString("D2"), minute.ToString("D2"), sec.ToString("D2"));
        }

        public SectionItem()
        {
            InitializeComponent();
        }

        private void RepeatButton_Click(object sender, RoutedEventArgs e)
        {
            SectionItemEventArgs arg = new SectionItemEventArgs(SectionItemEventArgs.eventtype.REPEAT);
            arg.Start = start;
            arg.End = end;
            SendEvent(arg);
            //MessageBox.Show(string.Format("{0}~{1}", StartText.Content, EndText.Content));
        }

        private void RecordStartButton_Click(object sender, RoutedEventArgs e)
        {
            SectionItemEventArgs arg = new SectionItemEventArgs(SectionItemEventArgs.eventtype.RECORDSTART);
            arg.Start = start;
            arg.End = end;
            SendEvent(arg);
        }

        private void RecordEndButton_Click(object sender, RoutedEventArgs e)
        {
            SectionItemEventArgs arg = new SectionItemEventArgs(SectionItemEventArgs.eventtype.RECORDEND);
            arg.Start = start;
            arg.End = end;
            SendEvent(arg);
        }
    }
}
